<?php
if(!defined('ACCESS')) {exit('Access denied.');}
class MachineConsumables extends Base {
	// 表名
	private static $table_name = 'consumables_usage';
	// 查询字段
	private static $columns = '*';

	public static function getTableName(){
		return parent::$table_prefix.self::$table_name;
	}
	
	//列表 
	public static function getAllMachineConsumables($condition) {
		$db=self::__instance();
		$sql="select ".self::$columns."  from ".self::getTableName()." ";
		if($condition !=''){
			$sql = $sql.$condition;
		}
		$list = $db->query($sql)->fetchAll();
		if ($list) {
			
			return $list;
		}
		return array ();
	}
	

	public static function getGroupBySerialNumber($serial_number) {
		if ( $serial_number == "" ) {
			return false;
		}
		$db=self::__instance();
		$condition['serial_number'] = $serial_number;
		$list = $db->select ( self::getTableName(), self::$columns, $condition );
		if ($list) {
			return $list [0];
		}
		return array ();
	}
	
	public static  function getDbField(){
		$db=self::__instance();
		$sql = "select COLUMN_NAME,column_comment from Information_schema.columns where table_name='".self::getTableName()."'";
		$list = $db->query ($sql)->fetchAll();;
		if ($list) {
			return $list;
		}
		return array ();
	}
	
	public static function addMachineConsumables($machine_data) {
		if (! $machine_data || ! is_array ( $machine_data )) {
			return false;
		}

		$db=self::__instance();
		$id = $db->insert ( self::getTableName(), $machine_data );
		return $id;
	}

	public static function getMachineConsumablesById($machine_id) {
		if (! $machine_id || ! is_numeric ( $machine_id )) {
			return false;
		}
		$db=self::__instance();
		$condition['machine_id'] = $machine_id;
		$list = $db->select ( self::getTableName(), self::$columns, $condition );
		if ($list) {
			return $list [0];
		}
		return array ();
	}
	
	public static function delMachineConsumables($machine_id) {
		if (! $machine_id || ! is_numeric ( $machine_id )) {
			return false;
		}
		$db=self::__instance();
		$condition = array("machine_id" => $machine_id);
		$result = $db->delete ( self::getTableName(), $condition );
		return $result;
	}
	
	public static function updateMachineConsumablesInfo($machine_id,$machine_data) {
		if (! $machine_data || ! is_array ( $machine_data )) {
			return false;
		}
		$db=self::__instance();
		$condition=array("machine_id"=>$machine_id);
		$id = $db->update ( self::getTableName(), $machine_data,$condition );

		return $id;
	}
	
	public static function getMachinesConsumablesUsage() {
		$db=self::__instance();
	
		$sql = " SELECT id,machine_id,consumable_item,market_value,single_amount,discount,beg_time,use_days,end_time,contract_number,usage_remark,create_user_id,create_time, TIMESTAMPDIFF(MONTH,NOW(),end_time) AS month_diff FROM  osa_consumables_usage  ";
		$list = $db->query ($sql)->fetchAll();;
		if ($list) {
			return $list;
		}
		return array ();
	}
	
// 	public static function getGroupByName($group_name) {
// 		if ( $group_name == "" ) {
// 			return false;
// 		}
// 		$db=self::__instance();
// 		$condition['group_name'] = $group_name;
// 		$list = $db->select ( self::getTableName(), self::$columns, $condition );
// 		if ($list) {
// 			return $list [0];
// 		}
// 		return array ();
// 	}
	
// 	public static function updateGroupInfo($group_id,$group_data) {
// 		if (! $group_data || ! is_array ( $group_data )) {
// 			return false;
// 		}
// 		$db=self::__instance();
// 		$condition=array("group_id"=>$group_id);
// 		$id = $db->update ( self::getTableName(), $group_data,$condition );
		
// 		return $id;
// 	}
	

	
// 	public static function getGroupForOptions() {
// 		$group_list = self::getAllGroup ();
		
// 		foreach ( $group_list as $group ) {
// 			$group_options_array [$group ['group_id']] = $group ['group_name'];
// 		}
		
// 		return $group_options_array;
// 	}
	
// 	public static function getGroupUsers($group_id) {
// 		$db=self::__instance();
// 		$sql="select ".self::$columns." ,u.user_id as user_id,u.user_name as user_name,u.real_name as real_name from ".self::getTableName()." g,".User::getTableName()." u where g.group_id = $group_id and g.group_id = u.user_group order by g.group_id,u.user_id";
// 		$list = $db->query ($sql)->fetchAll();
// 		if ($list) {
// 			return $list;
// 		}
// 		return array ();
// 	}
}
